﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DAO;
using DTO;
using System.Text.RegularExpressions;

namespace BUS
{
    public class DocGiaBUS
    {
        public static bool Them(DocGiaDTO dto)
        {
            //tạo chuỗi thông báo lỗi
            string thongBaoLoi = "";

            //kiểm tra hợp lệ
            if (KiemTra(dto, ref thongBaoLoi) == false)
            {
                throw new Exception(thongBaoLoi);
            }

            //thêm độc giả
            DocGiaDAO.Them(dto);

            return true;
        }

        private static bool KiemTra(DocGiaDTO dto, ref string thongBaoLoi)
        {
            //đọc tuổi tối đa, tuổi tối thiểu
            int tuoiToiDa = (int)ThamSoBUS.TuoiToiDa().GiaTri;
            int tuoiToiThieu = (int)ThamSoBUS.TuoiToiThieu().GiaTri;

            //tính số tuổi
            int soTuoi = dto.NgaySinh.Year;
            soTuoi = DateTime.Now.Year - soTuoi;

            //kiểm tra hợp lệ
            if (soTuoi > tuoiToiDa || soTuoi < tuoiToiThieu)
            {
                thongBaoLoi = "Số tuổi không phù hợp quy định";
                return false;
            }

            //kiểm tra email
            if (IsValidEmail(dto.Email) == false)
            {
                thongBaoLoi = "Email không hợp lệ";
                return false;
            }
            return true;
        }

        /// <summary>
        /// http://www.dreamincode.net/code/snippet1374.htm
        /// method for determining is the user provided a valid email address
        /// We use regular expressions in this check, as it is a more thorough
        /// way of checking the address provided
        /// </summary>
        /// <param name="email">email address to validate</param>
        /// <returns>true is valid, false if not valid</returns>
        private static bool IsValidEmail(string email)
        {
            //regular expression pattern for valid email
            //addresses, allows for the following domains:
            //com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tv
            string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
    (com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
            //Regular expression object
            Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
            //boolean variable to return to calling method
            bool valid = false;

            //make sure an email address was provided
            if (string.IsNullOrEmpty(email))
            {
                valid = false;
            }
            else
            {
                //use IsMatch to validate the address
                valid = check.IsMatch(email);
            }
            //return the value to the calling method
            return valid;
        }

        public static List<DocGiaDTO> DocDanhSach()
        {
            return DocGiaDAO.DocDanhSach();
        }

        public static bool CapNhatTienNo(DocGiaDTO dto)
        {
            DocGiaDAO.CapNhatTienNo(dto);
            return true;
        }
    }
}
